package com.jzo2o.orders.dispatch.test.strategys;

import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.orders.dispatch.model.dto.ServeProviderDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评分优先规则处理器类
 *
 * @Author mr.wu
 * @Date 2024-12-29 10:21
 */
@Slf4j
public class ScoreRule extends AbstractProcessRule{


    public ScoreRule(IProcessRule next) {
        super(next);
    }

    /**
     * 距离优先规则：细筛服务人员的逻辑
     * @param serveProviderDTOList
     * @return
     */
    public List<ServeProviderDTO> doFilter(List<ServeProviderDTO> serveProviderDTOList){
        log.info("评分优先规则，开始执行.....");
        if(CollUtils.size(serveProviderDTOList)<2){
            return serveProviderDTOList;
        }

        //对所有服务人员评分降序排序 (评分越高越优先)
        serveProviderDTOList = serveProviderDTOList.stream()
                .sorted(Comparator.comparing(ServeProviderDTO::getEvaluationScore).reversed()).collect(Collectors.toList());

        //获取排序后评分最高的第一个服务人员
        ServeProviderDTO first = CollUtils.getFirst(serveProviderDTOList);

        //从服务人员列表中筛选出跟第一个服务人员评分数一样的服务人员
        serveProviderDTOList = serveProviderDTOList.stream().filter(x-> x.getEvaluationScore().equals(first.getEvaluationScore())).collect(Collectors.toList());

        return serveProviderDTOList;
    }


}
